﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Algobox.Structure.Finance.Strategies.SpikeArb;
using Algobox.Structure.Finance.Strategies.Terrapin;
using Algobox.Objects.Finance;
using System.Collections.Concurrent;
using Algobox.Objects.Finance.Assets.Stock;
using Algobox.Structure.Finance.Strategies.Moss;
using Algobox.Structure.Finance.Strategies.Dastan;
using Algobox.Structure.Finance.Strategies.FractionPrototype;

namespace Algobox.Data.Finance
{
    public class BatsReplayTestData : ISpikeArbData, ITerrapinData, IMossData, IDastanData, IFractionData
    {
        public BatsReplayTestData(ConcurrentDictionary<string, StockSecurity> stocks)
        {
            _stocks = stocks;
            //LoadStocks();
        }

        ConcurrentDictionary<string, StockSecurity> _stocks;
        public IDictionary<string, StockSecurity> Stocks { get { return _stocks; } }

        public string FeedName
        {
            get { return "BatsReplayTestData"; }
        }

        public bool TryLoadStrategySpikeArb(SpikeArbManagement spikeArb)
        {
            StockSecurity stock;

            if (_stocks.TryGetValue("LLOYl", out stock))
            {
                spikeArb.AddOrUpdate(
                    0, stock, true, true, true,
                    false, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                    );
            }
            if (_stocks.TryGetValue("BARCl", out stock))
            {
                spikeArb.AddOrUpdate(
                1, stock, true, true, true,
                false, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                );
            }
            if (_stocks.TryGetValue("VODl", out stock))
            {
                spikeArb.AddOrUpdate(
                    1, stock, true, true, true,
                    false, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                    );
            }
            if (_stocks.TryGetValue("CPWl", out stock))
            {
                spikeArb.AddOrUpdate(
                1, stock, true, true, true,
                false, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                );
            }
            if (_stocks.TryGetValue("HSBAl", out stock))
            {
                spikeArb.AddOrUpdate(
                1, stock, true, true, true,
                false, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                );
            }
            return true;
        }

        public bool TrySaveStrategySpikeArb(SpikeArbManagement spikeArb)
        {
            return true;
        }

        public bool TryLoadStrategyTerrapin(TerrapinManagement manager)
        {
            StockSecurity security;
            TerrapinStock strategy;

            if (_stocks.TryGetValue("ISYSl", out security))
            {
                strategy = manager.AddOrUpdate(
                    Guid.NewGuid(),
                    security,
                    "LQD014",
                    1000,
                    2,
                    2,
                    2,
                    2);

                strategy.IsEnabled = true;
            }

            /*
            string con = String.Format("Data Source=|DataDirectory|\\{0}.sdf", typeof(Algobox.Data.Zephyr.Local.ZephyrLocalDB).Name);

            using (Algobox.Data.Zephyr.Local.ZephyrLocalDB linq = new Zephyr.Local.ZephyrLocalDB(con))
            {
                TerrapinStock strategy;
                foreach (Algobox.Data.Zephyr.Local.TerrapinSettings data in linq.TerrapinSettings.OrderBy(a => a.Symbol).OrderBy(a => a.EntryBars).OrderBy(a => a.ExitBars).OrderBy(a => a.FastEMA).OrderBy(a => a.SlowSMA))
                {
                    if (_stocks.TryGetValue(data.Symbol, out security))
                    {
                        strategy = manager.AddOrUpdate(
                            data.StrategyId,
                            security,
                            data.Account,
                            data.UnitValue,
                            data.EntryBars,
                            data.ExitBars,
                            data.FastEMA,
                            data.SlowSMA);

                        strategy.IsEnabled = data.IsOn;
                    }
                }
            }
            */
            return true;
        }

        public bool TrySaveStrategyTerrapin(TerrapinManagement manager)
        {
            /*
            try
            {
                string con = String.Format("Data Source=|DataDirectory|\\{0}.sdf", typeof(Algobox.Data.Zephyr.Local.ZephyrLocalDB).Name);

                using (Algobox.Data.Zephyr.Local.ZephyrLocalDB linq = new Zephyr.Local.ZephyrLocalDB(con))
                {
                    Algobox.Data.Zephyr.Local.TerrapinSettings data;

                    // remove deleted strategies
                    Guid[] ids = manager.Strategies.Keys.ToArray();
                    linq.TerrapinSettings.DeleteAllOnSubmit(linq.TerrapinSettings.Where(a => !ids.Contains(a.StrategyId)));

                    foreach (TerrapinStock strategy in manager.Strategies.Values)
                    {
                        data = linq.TerrapinSettings.SingleOrDefault(a => a.StrategyId == strategy.DatabaseId);
                        // update existing strategies
                        if (data != default(Algobox.Data.Zephyr.Local.TerrapinSettings))
                        {
                            data.Symbol = strategy.Security.Symbol;
                            data.Account = strategy.Account;
                            data.UnitValue = strategy.UnitValue;
                            data.EntryBars = strategy.EntryBars;
                            data.ExitBars = strategy.ExitBars;
                            data.FastEMA = strategy.FastEMA;
                            data.SlowSMA = strategy.SlowSMA;
                            data.IsOn = strategy.IsEnabled;
                        }
                        // add new strategy
                        else
                        {
                            data = new Zephyr.Local.TerrapinSettings();
                            data.StrategyId = strategy.DatabaseId;
                            data.Symbol = strategy.Security.Symbol;
                            data.Account = strategy.Account;
                            data.UnitValue = strategy.UnitValue;
                            data.EntryBars = strategy.EntryBars;
                            data.ExitBars = strategy.ExitBars;
                            data.FastEMA = strategy.FastEMA;
                            data.SlowSMA = strategy.SlowSMA;
                            data.IsOn = strategy.IsEnabled;
                            linq.TerrapinSettings.InsertOnSubmit(data);
                        }
                    }
                    linq.SubmitChanges();
                }
            }
            catch
            {
                return false;
            }
            */
            return true;
        }


        public bool TryLoadStrategyMoss(string username, MossManagement manager)
        {
            StockSecurity security;
            if (_stocks.TryGetValue("VODl", out security))
            {
                manager.AddOrUpdate(Guid.NewGuid(), security, "LQA001", 1000, 70, 30, 5, 14, 1.5);
            }

            return true;
        }

        public bool TrySaveStrategyMoss(string username, MossManagement manager)
        {
            return true;
        }
        

        public bool TryLoadStrategyDastan(DastanManagement manager)
        {
            StockSecurity security;
            DastanStock strategy;

            if (_stocks.TryGetValue("ISYSl", out security))
            {
                strategy = manager.AddOrUpdate(
                    Guid.NewGuid(),
                    security,
                    "LQD014",
                    1000,
                    2,
                    2,
                    2,
                    2);

                strategy.IsEnabled = true;
            }

            return true;
        }

        public bool TrySaveStrategyDastan(DastanManagement manager)
        {
            return true;
        }



        public StockSecurity AddStock(string symbol)
        {
            return _stocks.AddOrUpdate(symbol,
                (key) =>
                {
                    StockSecurity security = new StockSecurity(key, Markets.MarketMIC.XLON)
                    {
                        Currency = Currencies.Currency.GBX,
                        Index = "FTSE100",
                        Sector = "SectorB",
                        IndustryGroup = "IndustryB",
                        Industry = "IndustryGroupB1",
                        ISIN = "GB0000000000",
                        Phase = Phases.Phase.Open
                    };
                    security.Listings.TryAdd(Exchanges.ExchangeMIC.XLON, new StockListing(security, Exchanges.ExchangeMIC.CHIX, Algobox.Objects.Finance.Assets.TickStatic.Default(), 3));
                    security.Listings.TryAdd(Exchanges.ExchangeMIC.BATE, new StockListing(security, Exchanges.ExchangeMIC.CHIX, Algobox.Objects.Finance.Assets.TickStatic.Default(), 1));
                    security.Listings.TryAdd(Exchanges.ExchangeMIC.CHIX, new StockListing(security, Exchanges.ExchangeMIC.CHIX, Algobox.Objects.Finance.Assets.TickStatic.Default(), 2));
                    return security;
                },
                (key, security) =>
                {
                    security.Listings.TryAdd(Exchanges.ExchangeMIC.XLON, new StockListing(security, Exchanges.ExchangeMIC.CHIX, Algobox.Objects.Finance.Assets.TickStatic.Default(), 3));
                    security.Listings.TryAdd(Exchanges.ExchangeMIC.BATE, new StockListing(security, Exchanges.ExchangeMIC.CHIX, Algobox.Objects.Finance.Assets.TickStatic.Default(), 1));
                    security.Listings.TryAdd(Exchanges.ExchangeMIC.CHIX, new StockListing(security, Exchanges.ExchangeMIC.CHIX, Algobox.Objects.Finance.Assets.TickStatic.Default(), 2));
                    return security;
                });
        }


        private void LoadStocks()
        {
            AddStock("3INl");
            AddStock("AALl");
            AddStock("ABFl");
            AddStock("ABGl");
            AddStock("ADMl");
            AddStock("ADNl");
            AddStock("AFRl");
            AddStock("AGKl");
            AddStock("AGSl");
            AddStock("AHTl");
            AddStock("AMECl");
            AddStock("AMLl");
            AddStock("ANTOl");
            AddStock("AQPl");
            AddStock("ARIl");
            AddStock("ARMl");
            AddStock("ASHMl");
            AddStock("ASLl");
            AddStock("ATKl");
            AddStock("ATSTl");
            AddStock("AUl");
            AddStock("AVEl");
            AddStock("AVl");
            AddStock("AVVl");
            AddStock("AZNl");
            AddStock("BABl");
            AddStock("BABSl");
            AddStock("BAGl");
            AddStock("BAl");
            AddStock("BARCl");
            AddStock("BATSl");
            AddStock("BAYl");
            AddStock("BBAl");
            AddStock("BBAYl");
            AddStock("BBYl");
            AddStock("BDEVl");
            AddStock("BEZl");
            AddStock("BGCl");
            AddStock("BGl");
            AddStock("BHGGl");
            AddStock("BHGUl");
            AddStock("BHMEl");
            AddStock("BHMGl");
            AddStock("BHMUl");
            AddStock("BKGl");
            AddStock("BLNDl");
            AddStock("BLTl");
            AddStock("BNKRl");
            AddStock("BNZLl");
            AddStock("BOKl");
            AddStock("BOYl");
            AddStock("BPl");
            AddStock("BRBYl");
            AddStock("BREl");
            AddStock("BRWMl");
            AddStock("BSETl");
            AddStock("BSYl");
            AddStock("BTEMl");
            AddStock("BTl");
            AddStock("BTSMl");
            AddStock("BVICl");
            AddStock("BVSl");
            AddStock("BWNGl");
            AddStock("BWYl");
            AddStock("BYGl");
            AddStock("CAPCl");
            AddStock("CBGl");
            AddStock("CCCl");
            AddStock("CCLl");
            AddStock("CEYl");
            AddStock("CGLl");
            AddStock("CHGl");
            AddStock("CHTRl");
            AddStock("CKSNl");
            AddStock("CLDNl");
            AddStock("CLLNl");
            AddStock("CNAl");
            AddStock("CNEl");
            AddStock("COBl");
            AddStock("COLTl");
            AddStock("CPGl");
            AddStock("CPIl");
            AddStock("CPPl");
            AddStock("CPRl");
            AddStock("CRDAl");
            AddStock("CSCGl");
            AddStock("CSRl");
            AddStock("CTYl");
            AddStock("CWCl");
            AddStock("CWKl");
            AddStock("CWl");
            AddStock("CWWl");
            AddStock("DABl");
            AddStock("DCGl");
            AddStock("DDTl");
            AddStock("DEBl");
            AddStock("DGEl");
            AddStock("DJANl");
            AddStock("DLARl");
            AddStock("DLNl");
            AddStock("DMGTl");
            AddStock("DNLMl");
            AddStock("DNOl");
            AddStock("DNXl");
            AddStock("DOMl");
            AddStock("DRXl");
            AddStock("DTYl");
            AddStock("DVOl");
            AddStock("DVSGl");
            AddStock("DXNSl");
            AddStock("ECMl");
            AddStock("EDINl");
            AddStock("EFMl");
            AddStock("ELMl");
            AddStock("ELTAl");
            AddStock("EMGl");
            AddStock("ENQl");
            AddStock("ENRCl");
            AddStock("ERMl");
            AddStock("ESSRl");
            AddStock("ETIl");
            AddStock("EXPNl");
            AddStock("EZJl");
            AddStock("FCPTl");
            AddStock("FCPXl");
            AddStock("FCSSl");
            AddStock("FDSAl");
            AddStock("FENRl");
            AddStock("FEVl");
            AddStock("FGPl");
            AddStock("FLTRl");
            AddStock("FPl");
            AddStock("FPTl");
            AddStock("FRCLl");
            AddStock("FRESl");
            AddStock("FSVl");
            AddStock("FXPOl");
            AddStock("GFSl");
            AddStock("GKNl");
            AddStock("GNKl");
            AddStock("GNSl");
            AddStock("GOGl");
            AddStock("GPORl");
            AddStock("GRGl");
            AddStock("GRIl");
            AddStock("GRTl");
            AddStock("GSKl");
            AddStock("GSSl");
            AddStock("HASl");
            AddStock("HFDl");
            AddStock("HGGl");
            AddStock("HICLl");
            AddStock("HIKl");
            AddStock("HLCLl");
            AddStock("HLl");
            AddStock("HLMAl");
            AddStock("HMSOl");
            AddStock("HOCl");
            AddStock("HOILl");
            AddStock("HOMEl");
            AddStock("HSBAl");
            AddStock("HSNl");
            AddStock("HSVl");
            AddStock("HSXl");
            AddStock("HTGl");
            AddStock("HWDNl");
            AddStock("IAPl");
            AddStock("ICPl");
            AddStock("IEMl");
            AddStock("IGGl");
            AddStock("IHGl");
            AddStock("IIIl");
            AddStock("IMGl");
            AddStock("IMIl");
            AddStock("IMTl");
            AddStock("INCHl");
            AddStock("INFl");
            AddStock("INPPl");
            AddStock("INVPl");
            AddStock("IPFl");
            AddStock("IPRl");
            AddStock("ISATl");
            AddStock("ISYSl");
            AddStock("ITEl");
            AddStock("ITRKl");
            AddStock("ITVl");
            AddStock("JAIl");
            AddStock("JAMl");
            AddStock("JDl");
            AddStock("JDWl");
            AddStock("JESCl");
            AddStock("JIIl");
            AddStock("JKXl");
            AddStock("JLTl");
            AddStock("JMATl");
            AddStock("JMGl");
            AddStock("JUPl");
            AddStock("KAZl");
            AddStock("KESAl");
            AddStock("KGFl");
            AddStock("KIEl");
            AddStock("KLRl");
            AddStock("KMRl");
            AddStock("LADl");
            AddStock("LAMl");
            AddStock("LANDl");
            AddStock("LGENl");
            AddStock("LLOYl");
            AddStock("LMIl");
            AddStock("LOGl");
            AddStock("LRDl");
            AddStock("LREl");
            AddStock("LSEl");
            AddStock("LWDBl");
            AddStock("MABl");
            AddStock("MARSl");
            AddStock("MCBl");
            AddStock("MCROl");
            AddStock("MGCRl");
            AddStock("MGGTl");
            AddStock("MKSl");
            AddStock("MLCl");
            AddStock("MNDIl");
            AddStock("MNKSl");
            AddStock("MONYl");
            AddStock("MPIl");
            AddStock("MRCHl");
            AddStock("MRCl");
            AddStock("MROl");
            AddStock("MRSl");
            AddStock("MRWl");
            AddStock("MSYl");
            AddStock("MTCl");
            AddStock("MTOl");
            AddStock("MUTl");
            AddStock("MYIl");
            AddStock("NEXl");
            AddStock("NGl");
            AddStock("NWGl");
            AddStock("NXTl");
            AddStock("OCDOl");
            AddStock("OMLl");
            AddStock("PAGl");
            AddStock("PCTl");
            AddStock("PFCl");
            AddStock("PFDl");
            AddStock("PFGl");
            AddStock("PFLl");
            AddStock("PHNXl");
            AddStock("PICl");
            AddStock("PLIl");
            AddStock("PMOl");
            AddStock("PNNl");
            AddStock("POGl");
            AddStock("PRTYl");
            AddStock("PRUl");
            AddStock("PSNl");
            AddStock("PSONl");
            AddStock("PUBl");
            AddStock("PZCl");
            AddStock("QQl");
            AddStock("RATl");
            AddStock("RBl");
            AddStock("RBSl");
            AddStock("RCPl");
            AddStock("RDSAl");
            AddStock("RDSBl");
            AddStock("RDWl");
            AddStock("RELl");
            AddStock("REXl");
            AddStock("RGUl");
            AddStock("RIOl");
            AddStock("RMVl");
            AddStock("RNKl");
            AddStock("RORl");
            AddStock("RPSl");
            AddStock("RRl");
            AddStock("RRSl");
            AddStock("RSAl");
            AddStock("RSLl");
            AddStock("RSWl");
            AddStock("RTNl");
            AddStock("RTOl");
            AddStock("RWDl");
            AddStock("SABl");
            AddStock("SBRYl");
            AddStock("SCINl");
            AddStock("SDLl");
            AddStock("SDRCl");
            AddStock("SDRl");
            AddStock("SGCl");
            AddStock("SGEl");
            AddStock("SGPl");
            AddStock("SGROl");
            AddStock("SHBl");
            AddStock("SHIl");
            AddStock("SHPl");
            AddStock("SIAl");
            AddStock("SKSl");
            AddStock("SLl");
            AddStock("SMDRl");
            AddStock("SMDSl");
            AddStock("SMINl");
            AddStock("SMPl");
            AddStock("SMTl");
            AddStock("SMWHl");
            AddStock("SNl");
            AddStock("SNRl");
            AddStock("SPDl");
            AddStock("SPTl");
            AddStock("SPXl");
            AddStock("SRPl");
            AddStock("SSEl");
            AddStock("SSLl");
            AddStock("STANl");
            AddStock("STHRl");
            AddStock("STJl");
            AddStock("STOBl");
            AddStock("SVIl");
            AddStock("SVSl");
            AddStock("SVTl");
            AddStock("SXSl");
            AddStock("SYRl");
            AddStock("TALKl");
            AddStock("TALVl");
            AddStock("TATEl");
            AddStock("TCGl");
            AddStock("TCYl");
            AddStock("TEMl");
            AddStock("TLPRl");
            AddStock("TLWl");
            AddStock("TMPLl");
            AddStock("TOMKl");
            AddStock("TPKl");
            AddStock("TRYl");
            AddStock("TRYSl");
            AddStock("TSCOl");
            AddStock("TTl");
            AddStock("TWl");
            AddStock("UBMl");
            AddStock("UKCMl");
            AddStock("ULEl");
            AddStock("ULVRl");
            AddStock("UTGl");
            AddStock("UUl");
            AddStock("VCTl");
            AddStock("VEDl");
            AddStock("VODl");
            AddStock("VTGl");
            AddStock("WEIRl");
            AddStock("WGl");
            AddStock("WMHl");
            AddStock("WOSl");
            AddStock("WPPl");
            AddStock("WSMl");
            AddStock("WTANl");
            AddStock("WTBl");
            AddStock("XCHl");
            AddStock("XTAl");
            AddStock("YELLl");
            AddStock("YULCl");
        }


        bool IFractionData.TryLoadStrategyFraction(string username, FractionManagement manager)
        {
            throw new NotImplementedException();
        }

        bool IFractionData.TrySaveStrategyFraction(string username, FractionManagement manager)
        {
            throw new NotImplementedException();
        }

    }
}
